package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.heima.wemedia.util.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    FileStorageService fileStorageService;
    @Autowired
    private WmNewsMaterialService wmNewsMaterialService;

    @Override
    public Page<WmMaterial> findList(WmMaterialDto wmMaterialDto) {
        //先去创建page对象
        Page<WmMaterial> wmMaterialPage = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        //构建查询条件
        LambdaQueryWrapper<WmMaterial> wmMaterialLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wmMaterialLambdaQueryWrapper.eq(WmMaterial::getUserId, ThreadLocalUtil.get());
        wmMaterialLambdaQueryWrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        wmMaterialLambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        return this.page(wmMaterialPage,wmMaterialLambdaQueryWrapper);

    }

    @Override
    public void uploadPicture(MultipartFile multipartFile) throws IOException {
            String url = fileStorageService.uploadImgFile("", System.nanoTime() + "", multipartFile.getInputStream());
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setUserId(ThreadLocalUtil.get());
            wmMaterial.setUrl(url);
            this.save(wmMaterial);

    }

    @Override
    @Transactional
    public ResponseResult delPicture(Integer id) {
        if (id ==null){
            return ResponseResult.errorResult(501,"参数失效");
        }
        WmMaterial byId = this.getById(id);
        if(byId==null){
            return ResponseResult.errorResult(1002,"数据不存在");
        }
        LambdaQueryWrapper<WmNewsMaterial> wmNewsMaterialLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wmNewsMaterialLambdaQueryWrapper.eq(WmNewsMaterial::getMaterialId,id);
        List<WmNewsMaterial> list = wmNewsMaterialService.list(wmNewsMaterialLambdaQueryWrapper);
        if (list.size() !=0 ){
            return ResponseResult.errorResult(501,"文件删除失败有引用的文章");
        }else {

            fileStorageService.downLoadFile(byId.getUrl());
            this.removeById(id);
        }
        return ResponseResult.okResult("操作成功");
    }
}
